home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 010 / ls / ls.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  161 lines

  1. /*
  2. ** ls - List directory using unix style wildcards
  3. **
  4. **  usage: ls [-l] filename
  5. **
  6. ** -l is an optional flag which will cause the directory listing to
  7. ** be printed one-up with file size and date included.  Otherwise
  8. ** directory listing is printed two-up.  The listing is NOT
  9. ** sorted.
  10. **
  11. ** examples:
  12. **  ls *.c       print all files which end in ".c"
  13. **  ls -l a*     print all files which begin with "a"
  14. **  ls -l a??b*  print all files which begin with "a" followed by any
  15. **               two characters followed by "b" and then any other
  16. **               characters
  17. **
  18. ** Written by:    Rick Schaeffer    1/11/86 and placed in public domain.
  19. **                E. 13611 26th Ave.
  20. **                Spokane, Wa.  99216
  21. **                Compuserve ID: 70120,174
  22. **                Bytenet ID: ricks.
  23. */
  24.  
  25. #include "wildexp.h"
  26.  
  27. struct find fwk;
  28.  
  29. int        totfiles;
  30. long    totsize;
  31.  
  32. main(argc,argv)
  33. int        argc;
  34. char    *argv[];
  35. {
  36.     char    pattern[128];
  37.     int        retval,lflag;
  38.     char    *fferror();
  39.  
  40.     lflag = 0;
  41.     totfiles = 0;
  42.     totsize = 0;
  43.     if (argc <= 1)
  44.         strcpy(pattern,"*");
  45.     else {
  46.         if (argc == 2) {    
  47.             strncpy(pattern,argv[1],120);
  48.             if (strcmp(pattern,"-l") == 0) {
  49.                 strcpy(pattern,"*");
  50.                 lflag = 1;
  51.                 }
  52.             }
  53.         else {
  54.             strncpy(pattern,argv[2],120);
  55.             if (strcmp(argv[1],"-l") == 0)
  56.                 lflag = 1;
  57.             }
  58.         }
  59.     if (pattern[strlen(pattern)-1] == ':')
  60.         strcat(pattern,"*");
  61.     retval = findfirst(pattern,&fwk);
  62.     if (retval > 0) {
  63.         printf("ls: Error - %s\n",fferror(retval));
  64.         find_cleanup(&fwk);
  65.         exit(1);
  66.         }
  67.     while (retval <= 0) {
  68.         if (retval < 0)
  69.             break;
  70.         if (lflag)
  71.             print_long(&fwk);
  72.         else
  73.             print_short(&fwk);
  74.         retval = findnext(&fwk);
  75.         }
  76.     printf("\n");
  77.     if (lflag)
  78.         printf("%d Files containing %ld bytes.\n",totfiles,totsize);
  79.     find_cleanup(&fwk);
  80. }
  81.  
  82. char *dates(s,dss)
  83. char    *s;
  84. struct DateStamp *dss;
  85. {
  86.     int    year,month,day;
  87.     static char dpm[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  88.  
  89.     year = 1978;
  90.     day = dss->ds_Days;
  91.     while (day >= 366) {
  92.         if ((year-1976) % 4 == 0) {
  93.             day -= 366;
  94.             year++;
  95.             }
  96.         else
  97.             if ((year-1976) % 4 != 0 && day >= 365) {
  98.                 day -= 365;
  99.                 year++;
  100.                 }
  101.         }
  102.     if ((year-1976) % 4 == 0) {
  103.         day = day % 366;
  104.         dpm[2] = 29;
  105.         }
  106.     else {
  107.         day = day % 365;
  108.         dpm[2] = 28;
  109.         }
  110.     for (month = 0; day > dpm[month]; month++)
  111.         day -= dpm[month];
  112.     sprintf(s,"%02d/%02d/%04d",month+1,day,year);
  113.     return(s);
  114. }
  115.  
  116. char *times(s,dss)
  117. char    *s;
  118. struct DateStamp *dss;
  119. {
  120.     int    hours,minutes,seconds;
  121.  
  122.     seconds = dss->ds_Tick / 50;
  123.     seconds %= 60;
  124.     minutes = dss->ds_Minute;
  125.     hours = minutes / 60;
  126.     minutes %= 60;
  127.     sprintf(s,"%02d:%02d:%02d",hours,minutes,seconds);
  128.     return(s);
  129. }
  130.  
  131. print_long(fwk)
  132. struct find *fwk;
  133. {
  134.     struct FileInfoBlock *f;
  135.     char    s[12],t[12];
  136.  
  137.     f = fwk->fp;
  138.     if (f->fib_DirEntryType < 0) {
  139.         printf("%-30s %7d %s %s\n",f->fib_FileName,f->fib_Size,
  140.             dates(s,&f->fib_Date),times(t,&f->fib_Date));
  141.         totsize += f->fib_Size;
  142.         }
  143.     else
  144.         printf("%-30s <DIR>   %s\n",f->fib_FileName,dates(s,&f->fib_Date));
  145.     totfiles++;
  146. }
  147.  
  148. print_short(fwk)
  149. struct find *fwk;
  150. {
  151.     static int f=0;
  152.  
  153.     printf("%-30s ",fwk->fname);
  154.     if (f == 0)
  155.         f = 1;
  156.     else {
  157.         printf("\n");
  158.         f = 0;
  159.         }
  160. }
  161.